<?xml version="1.0"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head><title>SASAPI</title><script src="../../../quicknav.js" type="text/javascript"></script><script><!--
document.quicknavBasePath="../../../index-files";
//--></script><link href="../../../style.css" title="JavaDoc" rel="stylesheet" type="text/css"/><link href="../../../unnatural.css" title="Unnatural" rel="alternate stylesheet" type="text/css"/><link href="../../../overview-summary.html" title="Overview" rel="top"/><meta name="generator" content="http://www.badgers-in-foil.co.uk/projects/as2api/"/></head><body><pre><span class="lineno" id="1">     1  </span><span class="comment doc">/**
<span class="lineno" id="2">     2  </span> * com.sekati.utils.DateUtils
<span class="lineno" id="3">     3  </span> * @version 1.0.7
<span class="lineno" id="4">     4  </span> * @author jason m horwitz | sekati.com
<span class="lineno" id="5">     5  </span> * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
<span class="lineno" id="6">     6  </span> * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
<span class="lineno" id="7">     7  </span> */</span>
<span class="lineno" id="8">     8  </span> 
<span class="lineno" id="9">     9  </span><span class="key">import</span> com.sekati.convert.TimeConversion;
<span class="lineno" id="10">    10  </span><span class="key">import</span> com.sekati.math.Range;
<span class="lineno" id="11">    11  </span>
<span class="lineno" id="12">    12  </span><span class="comment doc">/**
<span class="lineno" id="13">    13  </span> * Static function for handling dates from db and converting them into readable strings. Note that days &amp; months are 0-indexed.
<span class="lineno" id="14">    14  </span> * 
<span class="lineno" id="15">    15  </span> * {@code Usage:
<span class="lineno" id="16">    16  </span> * var d = DateUtils.dateFromDB("2006-06-01 12:10:45");
<span class="lineno" id="17">    17  </span> * trace(DateUtils.days[d.getDay()] + ", " + DateUtils.months[d.getMonth()] + " " + d.getDate() + ", " + (d.getHours()%12) + ":" + DateUtils.padTime(d.getMinutes()) + ((d.getHours() &gt; 12) ? "pm" :"am"));
<span class="lineno" id="18">    18  </span> * }
<span class="lineno" id="19">    19  </span> */</span>
<span class="lineno" id="20">    20  </span><span class="key">class</span> com.sekati.utils.DateUtils {
<span class="lineno" id="21">    21  </span>
<span class="lineno" id="22">    22  </span>	<span class="comment doc">/**
<span class="lineno" id="23">    23  </span>	 * Gets 0 indexed array of months for use with Date.getMonth()
<span class="lineno" id="24">    24  </span>	 * @return Array
<span class="lineno" id="25">    25  </span>	 */</span>
<span class="lineno" id="26">    26  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> get months():Array {
<span class="lineno" id="27">    27  </span>		<span class="key">return</span> <span class="key">new</span> Array( <span class="str_const">"January"</span>, <span class="str_const">"February"</span>, <span class="str_const">"March"</span>, <span class="str_const">"April"</span>, <span class="str_const">"May"</span>, <span class="str_const">"June"</span>, <span class="str_const">"July"</span>, <span class="str_const">"August"</span>, <span class="str_const">"September"</span>, <span class="str_const">"October"</span>, <span class="str_const">"November"</span>, <span class="str_const">"December"</span> );
<span class="lineno" id="28">    28  </span>	}
<span class="lineno" id="29">    29  </span>
<span class="lineno" id="30">    30  </span>	<span class="comment doc">/**
<span class="lineno" id="31">    31  </span>	 * Gets 0 indexed array of short months for use with Date.getMonth()
<span class="lineno" id="32">    32  </span>	 * @return Array
<span class="lineno" id="33">    33  </span>	 */</span>
<span class="lineno" id="34">    34  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> get shortmonths():Array {
<span class="lineno" id="35">    35  </span>		<span class="key">return</span> <span class="key">new</span> Array( <span class="str_const">"Jan"</span>, <span class="str_const">"Feb"</span>, <span class="str_const">"Mar"</span>, <span class="str_const">"Apr"</span>, <span class="str_const">"May"</span>, <span class="str_const">"Jun"</span>, <span class="str_const">"Jul"</span>, <span class="str_const">"Aug"</span>, <span class="str_const">"Sep"</span>, <span class="str_const">"Oct"</span>, <span class="str_const">"Nov"</span>, <span class="str_const">"Dec"</span> );	
<span class="lineno" id="36">    36  </span>	}
<span class="lineno" id="37">    37  </span>
<span class="lineno" id="38">    38  </span>	<span class="comment doc">/**
<span class="lineno" id="39">    39  </span>	 * Gets 0 indexed array of days for use with Date.getDay()
<span class="lineno" id="40">    40  </span>	 * @return Array
<span class="lineno" id="41">    41  </span>	 */</span>
<span class="lineno" id="42">    42  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> get days():Array {
<span class="lineno" id="43">    43  </span>		<span class="key">return</span> <span class="key">new</span> Array( <span class="str_const">"Sunday"</span>, <span class="str_const">"Monday"</span>, <span class="str_const">"Tuesday"</span>, <span class="str_const">"Wednesday"</span>, <span class="str_const">"Thursday"</span>, <span class="str_const">"Friday"</span>, <span class="str_const">"Saturday"</span> );
<span class="lineno" id="44">    44  </span>	}
<span class="lineno" id="45">    45  </span>
<span class="lineno" id="46">    46  </span>	<span class="comment doc">/**
<span class="lineno" id="47">    47  </span>	 * Get 0 indexed array of days for use with Date.getDay();
<span class="lineno" id="48">    48  </span>	 * @return Array
<span class="lineno" id="49">    49  </span>	 */</span>
<span class="lineno" id="50">    50  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> get shortdays():Array {
<span class="lineno" id="51">    51  </span>		<span class="key">return</span> <span class="key">new</span> Array( <span class="str_const">"Sun"</span>, <span class="str_const">"Mon"</span>, <span class="str_const">"Tue"</span>, <span class="str_const">"Wed"</span>, <span class="str_const">"Thur"</span>, <span class="str_const">"Fri"</span>, <span class="str_const">"Sat"</span> );
<span class="lineno" id="52">    52  </span>	}
<span class="lineno" id="53">    53  </span>
<span class="lineno" id="54">    54  </span>	<span class="comment doc">/**
<span class="lineno" id="55">    55  </span>	 * Get the month name by month number.
<span class="lineno" id="56">    56  </span>	 * @param n (Number)
<span class="lineno" id="57">    57  </span>	 * @return String
<span class="lineno" id="58">    58  </span>	 * {@code Usage:
<span class="lineno" id="59">    59  </span>	 * 	trace(DateUtils.getMonth(0); // returns "January"
<span class="lineno" id="60">    60  </span>	 * }
<span class="lineno" id="61">    61  </span>	 */</span>
<span class="lineno" id="62">    62  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> getMonth(n:Number):String {
<span class="lineno" id="63">    63  </span>		<span class="key">return</span> months[n];	
<span class="lineno" id="64">    64  </span>	}
<span class="lineno" id="65">    65  </span>
<span class="lineno" id="66">    66  </span>	<span class="comment doc">/**
<span class="lineno" id="67">    67  </span>	 * Get the short month name by month number.
<span class="lineno" id="68">    68  </span>	 * @param n (Number)
<span class="lineno" id="69">    69  </span>	 * @return String
<span class="lineno" id="70">    70  </span>	 * {@code Usage:
<span class="lineno" id="71">    71  </span>	 * 	trace(DateUtils.getShortMonth(0); // returns "Jan"
<span class="lineno" id="72">    72  </span>	 * }
<span class="lineno" id="73">    73  </span>	 */</span>
<span class="lineno" id="74">    74  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> getShortMonth(n:Number):String {
<span class="lineno" id="75">    75  </span>		<span class="key">return</span> shortmonths[n];	
<span class="lineno" id="76">    76  </span>	}
<span class="lineno" id="77">    77  </span>
<span class="lineno" id="78">    78  </span>	<span class="comment doc">/**
<span class="lineno" id="79">    79  </span>	 * Get the day name by day number.
<span class="lineno" id="80">    80  </span>	 * @param n (Number)
<span class="lineno" id="81">    81  </span>	 * @return String
<span class="lineno" id="82">    82  </span>	 * {@code Usage:
<span class="lineno" id="83">    83  </span>	 * 	trace(DateUtils.getDay(0); // returns "Sunday"
<span class="lineno" id="84">    84  </span>	 * }
<span class="lineno" id="85">    85  </span>	 */</span>	
<span class="lineno" id="86">    86  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> getDay(n:Number):String {
<span class="lineno" id="87">    87  </span>		<span class="key">return</span> days[n];	
<span class="lineno" id="88">    88  </span>	}
<span class="lineno" id="89">    89  </span>
<span class="lineno" id="90">    90  </span>	<span class="comment doc">/**
<span class="lineno" id="91">    91  </span>	 * Get the short day name by day number.
<span class="lineno" id="92">    92  </span>	 * @param n (Number)
<span class="lineno" id="93">    93  </span>	 * @return String
<span class="lineno" id="94">    94  </span>	 * {@code Usage:
<span class="lineno" id="95">    95  </span>	 * 	trace(DateUtils.getShortDay(0); // returns "Sun"
<span class="lineno" id="96">    96  </span>	 * }
<span class="lineno" id="97">    97  </span>	 */</span>	
<span class="lineno" id="98">    98  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> getShortDay(n:Number):String {
<span class="lineno" id="99">    99  </span>		<span class="key">return</span> shortdays[n];
<span class="lineno" id="100">   100  </span>	}
<span class="lineno" id="101">   101  </span>
<span class="lineno" id="102">   102  </span>	<span class="comment doc">/**
<span class="lineno" id="103">   103  </span>	 * Pads hours, Minutes or Seconds with a leading 0, 12:01 doesn't end up 12:1
<span class="lineno" id="104">   104  </span>	 * @param n (Number)
<span class="lineno" id="105">   105  </span>	 * @return String
<span class="lineno" id="106">   106  </span>	 */</span>
<span class="lineno" id="107">   107  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> padTime(n:Number):String {
<span class="lineno" id="108">   108  </span>		<span class="key">return</span> (String( n ).length &lt; <span class="num_const">2</span>) ? (<span class="str_const">"0"</span> + n) : n;
<span class="lineno" id="109">   109  </span>	}
<span class="lineno" id="110">   110  </span>
<span class="lineno" id="111">   111  </span>	<span class="comment doc">/**
<span class="lineno" id="112">   112  </span>	 * converts a DB formatted date string into a Flash Date Object.
<span class="lineno" id="113">   113  </span>	 * @param dbDate (String) date in YYYY-MM-DD HH:MM:SS format
<span class="lineno" id="114">   114  </span>	 * @return Date
<span class="lineno" id="115">   115  </span>	 */</span>
<span class="lineno" id="116">   116  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> dateFromDB(dbdate:String):Date {
<span class="lineno" id="117">   117  </span>		<span class="key">var</span> tmp:Array = dbdate.split( <span class="str_const">" "</span> );
<span class="lineno" id="118">   118  </span>		<span class="key">var</span> dates:Array = tmp[<span class="num_const">0</span>].split( <span class="str_const">"-"</span> );
<span class="lineno" id="119">   119  </span>		<span class="key">var</span> hours:Array = tmp[<span class="num_const">1</span>].split( <span class="str_const">":"</span> );
<span class="lineno" id="120">   120  </span>		<span class="key">var</span> d:Date = <span class="key">new</span> Date( dates[<span class="num_const">0</span>], dates[<span class="num_const">1</span>] - <span class="num_const">1</span>, dates[<span class="num_const">2</span>], hours[<span class="num_const">0</span>], hours[<span class="num_const">1</span>], hours[<span class="num_const">2</span>] );
<span class="lineno" id="121">   121  </span>		<span class="key">return</span> d;
<span class="lineno" id="122">   122  </span>	}
<span class="lineno" id="123">   123  </span>
<span class="lineno" id="124">   124  </span>	<span class="comment doc">/**
<span class="lineno" id="125">   125  </span>	 * Takes 24hr hours and converts to 12 hour with am/pm.
<span class="lineno" id="126">   126  </span>	 * @param hour24 (Number)
<span class="lineno" id="127">   127  </span>	 * @return Object
<span class="lineno" id="128">   128  </span>	 */</span>
<span class="lineno" id="129">   129  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> getHoursAmPm(hour24:Number):Object {
<span class="lineno" id="130">   130  </span>		<span class="key">var</span> returnObj:Object = <span class="key">new</span> Object( );
<span class="lineno" id="131">   131  </span>		returnObj.ampm = (hour24 &lt; <span class="num_const">12</span>) ? <span class="str_const">"am"</span> : <span class="str_const">"pm"</span>;
<span class="lineno" id="132">   132  </span>		<span class="key">var</span> hour12:Number = hour24 % <span class="num_const">12</span>;
<span class="lineno" id="133">   133  </span>		<span class="key">if</span> (hour12 == <span class="num_const">0</span>) {
<span class="lineno" id="134">   134  </span>			hour12 = <span class="num_const">12</span>;
<span class="lineno" id="135">   135  </span>		}
<span class="lineno" id="136">   136  </span>		returnObj.hours = hour12;
<span class="lineno" id="137">   137  </span>		<span class="key">return</span> returnObj;
<span class="lineno" id="138">   138  </span>	}
<span class="lineno" id="139">   139  </span>
<span class="lineno" id="140">   140  </span>	<span class="comment doc">/**
<span class="lineno" id="141">   141  </span>	 * Get the differences between two Dates in milliseconds.
<span class="lineno" id="142">   142  </span>	 * @param d1 (Date) 
<span class="lineno" id="143">   143  </span>	 * @param d2 (Date) optional [default: now]
<span class="lineno" id="144">   144  </span>	 * @return Number - difference between two dates in ms
<span class="lineno" id="145">   145  </span>	 */</span>
<span class="lineno" id="146">   146  </span>	 
<span class="lineno" id="147">   147  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> dateDiff(d1:Date, d2:Date):Number {
<span class="lineno" id="148">   148  </span>		<span class="key">if</span>(d2 == <span class="key">null</span>) d2 = <span class="key">new</span> Date( );
<span class="lineno" id="149">   149  </span>		<span class="key">return</span> d2.getTime( ) - d1.getTime( );
<span class="lineno" id="150">   150  </span>	}
<span class="lineno" id="151">   151  </span>
<span class="lineno" id="152">   152  </span>	<span class="comment doc">/**
<span class="lineno" id="153">   153  </span>	 * Check if birthdate entered meets required age.
<span class="lineno" id="154">   154  </span>	 * @param year (Number)
<span class="lineno" id="155">   155  </span>	 * @param month (Number)
<span class="lineno" id="156">   156  </span>	 * @param day (Number)
<span class="lineno" id="157">   157  </span>	 * @param requiredAge (Number)
<span class="lineno" id="158">   158  </span>	 * @return Boolean
<span class="lineno" id="159">   159  </span>	 */</span>
<span class="lineno" id="160">   160  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> isValidAge(year:Number, month:Number, day:Number, requiredAge:Number):Boolean {
<span class="lineno" id="161">   161  </span>		<span class="key">if</span> (!isValidDate( year, month, day, <span class="key">true</span> )) <span class="key">return</span> <span class="key">false</span>;
<span class="lineno" id="162">   162  </span>		<span class="key">var</span> now:Date = <span class="key">new</span> Date( );
<span class="lineno" id="163">   163  </span>		<span class="key">var</span> bd:Date = <span class="key">new</span> Date( year + requiredAge, month, day );
<span class="lineno" id="164">   164  </span>		<span class="key">return</span> (now.getTime( ) &gt; bd.getTime( ));
<span class="lineno" id="165">   165  </span>	}
<span class="lineno" id="166">   166  </span>
<span class="lineno" id="167">   167  </span>	<span class="comment doc">/**
<span class="lineno" id="168">   168  </span>	 * Check if a valid date can be created with inputs.
<span class="lineno" id="169">   169  </span>	 * @param year (Number)
<span class="lineno" id="170">   170  </span>	 * @param month (Number)
<span class="lineno" id="171">   171  </span>	 * @param day (Number)
<span class="lineno" id="172">   172  </span>	 * @param mustBeInPast (Boolean)
<span class="lineno" id="173">   173  </span>	 * @return Boolean
<span class="lineno" id="174">   174  </span>	 */</span>
<span class="lineno" id="175">   175  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> isValidDate(year:Number, month:Number, day:Number, mustBeInPast:Boolean):Boolean {
<span class="lineno" id="176">   176  </span>		<span class="key">if</span>(!Range.isInRange( year, <span class="num_const">1800</span>, <span class="num_const">3000</span> ) || isNaN( year )) <span class="key">return</span> <span class="key">false</span>;
<span class="lineno" id="177">   177  </span>		<span class="key">if</span>(!Range.isInRange( month, <span class="num_const">0</span>, <span class="num_const">11</span> ) || isNaN( month )) <span class="key">return</span> <span class="key">false</span>;
<span class="lineno" id="178">   178  </span>		<span class="key">if</span>(!Range.isInRange( day, <span class="num_const">1</span>, <span class="num_const">31</span> ) || isNaN( day )) <span class="key">return</span> <span class="key">false</span>;
<span class="lineno" id="179">   179  </span>		<span class="key">if</span>(day &gt; getTotalDaysInMonth( year, month )) <span class="key">return</span> <span class="key">false</span>;
<span class="lineno" id="180">   180  </span>		<span class="key">if</span>(mustBeInPast &amp;&amp; dateDiff( <span class="key">new</span> Date( year, month, day ) ) &lt; <span class="num_const">0</span>) <span class="key">return</span> <span class="key">false</span>;
<span class="lineno" id="181">   181  </span>		<span class="key">return</span> <span class="key">true</span>;
<span class="lineno" id="182">   182  </span>	}	
<span class="lineno" id="183">   183  </span>
<span class="lineno" id="184">   184  </span>	<span class="comment doc">/**
<span class="lineno" id="185">   185  </span>	 * Return the number of dates in a specific month.
<span class="lineno" id="186">   186  </span>	 * @param year (Number)
<span class="lineno" id="187">   187  </span>	 * @param month (Number)
<span class="lineno" id="188">   188  </span>	 * @return Number
<span class="lineno" id="189">   189  </span>	 */</span>
<span class="lineno" id="190">   190  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> getTotalDaysInMonth(year:Number, month:Number):Number {
<span class="lineno" id="191">   191  </span>		<span class="key">return</span> TimeConversion.ms2days( dateDiff( <span class="key">new</span> Date( year, month, <span class="num_const">1</span> ), <span class="key">new</span> Date( year, month + <span class="num_const">1</span>, <span class="num_const">1</span> ) ) );
<span class="lineno" id="192">   192  </span>	}
<span class="lineno" id="193">   193  </span>
<span class="lineno" id="194">   194  </span>	<span class="comment doc">/**
<span class="lineno" id="195">   195  </span>	 * Returns the number of days in a specific year.
<span class="lineno" id="196">   196  </span>	 * @param year (Number)
<span class="lineno" id="197">   197  </span>	 * @return Number
<span class="lineno" id="198">   198  </span>	 */</span>
<span class="lineno" id="199">   199  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> getTotalDaysInYear(year:Number):Number {
<span class="lineno" id="200">   200  </span>		<span class="key">return</span> TimeConversion.ms2days( dateDiff( <span class="key">new</span> Date( year, <span class="num_const">0</span>, <span class="num_const">1</span> ), <span class="key">new</span> Date( year + <span class="num_const">1</span>, <span class="num_const">0</span>, <span class="num_const">1</span> ) ) );
<span class="lineno" id="201">   201  </span>	}
<span class="lineno" id="202">   202  </span>
<span class="lineno" id="203">   203  </span>	<span class="key">private</span> <span class="key">function</span> DateUtils() {
<span class="lineno" id="204">   204  </span>	}
<span class="lineno" id="205">   205  </span>}</pre><ul class="main_nav" id="main_nav"><li><a href="../../../overview-summary.html" title="Overview of SASAPI" class="button">Overview</a></li><li><a href="package-summary.html" title="Overview of package com.sekati.utils" class="button">Package</a></li><li><a href="DateUtils.html" title="Detail of com.sekati.utils.DateUtils API" class="button">Class</a></li><li><span class="button nav_current">Source</span></li><li><a href="../../../index-files/index.html" title="Alpabetical index of types and members" class="button">Index</a></li></ul><div class="footer"><a href="http://www.badgers-in-foil.co.uk/projects/as2api/" title="ActionScript 2 API Documentation Generator">as2api</a></div></body></html>